package com.zhz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhz.domain.SysPrivilege;
import com.zhz.mapper.SysPrivilegeMapper;
import com.zhz.service.SysPrivilegeService;
import org.springframework.util.CollectionUtils;

/**
 * @author ：zhz
 * @date ：Created in 2020/12/20
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description: 
 **/
@Service
public class SysPrivilegeServiceImpl extends ServiceImpl<SysPrivilegeMapper, SysPrivilege> implements SysPrivilegeService{

    @Autowired
    private SysPrivilegeMapper sysPrivilegeMapper;
    /**
     *
     * @param menuId 菜单的ID
     * @param roleId 当前的查询的角色ID
     * @return
     */
    @Override
    public List<SysPrivilege> getAllSysPrivilege(Long menuId, Long roleId) {
        //1、查询所有的该菜单下的权限
        List<SysPrivilege> sysPrivileges = list(new LambdaQueryWrapper<SysPrivilege>().eq(SysPrivilege::getMenuId, menuId));
        if (CollectionUtils.isEmpty(sysPrivileges)){
            return Collections.emptyList();
        }
        //2、当前传递的角色使用包含该权限信息也要放进去
        for (SysPrivilege sysPrivilege : sysPrivileges) {
            Set<Long> currentRoleSysPrivileges=sysPrivilegeMapper.getPrivilegesByRole(roleId);
            if (currentRoleSysPrivileges.contains(sysPrivilege.getId())){
                sysPrivilege.setOwn(1);//当前的角色是否有该权限
            }
        }
        return sysPrivileges;
    }
}
